﻿using Common.AspNetCore;
using Common.Infrastructure;
using FCP.Api.Repository;
using FCP.Api.Utils;
using FCP.Data.Entity;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FCP.Api.Service
{
    public class FCPDataService : CommonService
    {
        private readonly FCPService _fcpService;
        private readonly string lang;
        private readonly FCPRepository fcpRepository;
        public FCPDataService(IServiceProvider provider, FCPService auth, IConfiguration c, FCPRepository fcp) : base(provider)
        {
            this._fcpService = auth;
            this.lang = !string.IsNullOrEmpty(c["DefaultLang"]) ? c["DefaultLang"] : "CN";
            this.fcpRepository = fcp;
        }



        public async Task<string> OrderPageByParam(string entityName, ApiOrderPageParam apiOrderPageParam)
        {
            if (string.IsNullOrEmpty(entityName) || apiOrderPageParam == null) return string.Empty;
            switch (entityName)
            {
                case "t_search":
                    {
                        var r = await OrderPageSearch(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_search_field":
                    {
                        var r = await OrderPageSearchField(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_search_field_filter_field":
                    {
                        var r = await OrderPageSearchFieldFilterField(apiOrderPageParam);
                        if (r != null) return r.ToJson(); 
                    }
                    break;
                case "t_chart":
                    {
                        var r = await OrderPageChart(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_custom":
                    {
                        var r = await OrderPageCustom(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_custom_btn":
                    {
                        var r = await OrderPageCustomBtn(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_edit":
                    {
                        var r = await OrderPageEdit(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_edit_field":
                    {
                        var r = await OrderPageEditField(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_edit_field_filter_field":
                    {
                        var r = await OrderPageEditFieldFilterField(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_edit_field_map":
                    {
                        var r = await OrderPageEditFieldMap(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_edit_field_map_detail":
                    {
                        var r = await OrderPageEditFieldMapDetail(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_edit_panel":
                    {
                        var r = await OrderPageEditPanel(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_edit_btn":
                    {
                        var r = await OrderPageEditBtn(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_table":
                    {
                        var r = await OrderPageTable(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_table_field":
                    {
                        var r = await OrderPageTableField(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_table_field_filter_field":
                    {
                        var r = await OrderPageTableFieldFilterField(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_table_filter_field":
                    {
                        var r = await OrderPageTableFilterField(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_table_panel":
                    {
                        var r = await OrderPageTablePanel(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_table_btn":
                    {
                        var r = await OrderPageTableBtn(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_tree":
                    {
                        var r = await OrderPageTree(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_tree_field":
                    {
                        var r = await OrderPageTreeField(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_tree_btn":
                    {
                        var r = await OrderPageTreeBtn(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_form":
                    {
                        var r = await OrderPageForm(apiOrderPageParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
            }
            return string.Empty;
        }

        public async Task<bool> FullUpdateByParam(string entityName, string entityStr)
        {
            if (string.IsNullOrEmpty(entityName) || string.IsNullOrEmpty(entityStr)) return false;
            switch (entityName)
            {
                case "t_search":
                    {
                        return await FullUpdateSearch(entityStr.ToObjectFromJson<t_search>());

                    }
                case "t_search_field":
                    {
                        return await FullUpdateSearchField(entityStr.ToObjectFromJson<t_search_field>());

                    }
                case "t_search_field_filter_field":
                    {
                        return await FullUpdateSearchFieldFilterField(entityStr.ToObjectFromJson<t_search_field_filter_field>());

                    } 
                case "t_chart":
                    {
                        return await FullUpdateChart(entityStr.ToObjectFromJson<t_chart>());

                    }
                case "t_custom":
                    {
                        return await FullUpdateCustom(entityStr.ToObjectFromJson<t_custom>());

                    }
                case "t_custom_btn":
                    {
                        return await FullUpdateCustomBtn(entityStr.ToObjectFromJson<t_custom_btn>());

                    }
                case "t_edit":
                    {
                        return await FullUpdateEdit(entityStr.ToObjectFromJson<t_edit>());

                    }
                case "t_edit_field":
                    {
                        return await FullUpdateEditField(entityStr.ToObjectFromJson<t_edit_field>());

                    }
                case "t_edit_field_filter_field":
                    {
                        return await FullUpdateEditFieldFilterField(entityStr.ToObjectFromJson<t_edit_field_filter_field>());

                    }
                case "t_edit_field_map":
                    {
                        return await FullUpdateEditFieldMap(entityStr.ToObjectFromJson<t_edit_field_map>());

                    }
                case "t_edit_field_map_detail":
                    {
                        return await FullUpdateEditFieldMapDetail(entityStr.ToObjectFromJson<t_edit_field_map_detail>());

                    }
                case "t_edit_panel":
                    {
                        return await FullUpdateEditPanel(entityStr.ToObjectFromJson<t_edit_panel>());

                    }
                case "t_edit_btn":
                    {
                        return await FullUpdateEditBtn(entityStr.ToObjectFromJson<t_edit_btn>());

                    }
                case "t_table":
                    {
                        LibRedisCacheUtils.Delete(Provider, $"statis_used_form_tables_{UserToken?.OrgId}");
                        return await FullUpdateTable(entityStr.ToObjectFromJson<t_table>());

                    }
                case "t_table_field":
                    {
                        return await FullUpdateTableField(entityStr.ToObjectFromJson<t_table_field>());

                    }
                case "t_table_field_filter_field":
                    {
                        return await FullUpdateTableFieldFilterField(entityStr.ToObjectFromJson<t_table_field_filter_field>());

                    }
                case "t_table_filter_field":
                    {
                        return await FullUpdateTableFilterField(entityStr.ToObjectFromJson<t_table_filter_field>());

                    }
                case "t_table_panel":
                    {
                        return await FullUpdateTablePanel(entityStr.ToObjectFromJson<t_table_panel>());

                    }
                case "t_table_btn":
                    {
                        return await FullUpdateTableBtn(entityStr.ToObjectFromJson<t_table_btn>());

                    }
                case "t_tree":
                    {
                        return await FullUpdateTree(entityStr.ToObjectFromJson<t_tree>());

                    }
                case "t_tree_field":
                    {
                        return await FullUpdateTreeField(entityStr.ToObjectFromJson<t_tree_field>());

                    }
                case "t_tree_btn":
                    {
                        return await FullUpdateTreeBtn(entityStr.ToObjectFromJson<t_tree_btn>());

                    }

                case "t_form":
                    {
                        LibRedisCacheUtils.Delete(Provider, $"statis_used_form_tables_{UserToken?.OrgId}");
                        var form = entityStr.ToObjectFromJson<t_form>();
                        LibRedisCacheUtils.Delete(Provider, $"fcp_form_info_{form.id}");
                        return await FullUpdateForm(form);
                    }
            }

            return false;
        }

        public async Task<string> GetByParam(string entityName, string id)
        {
            if (string.IsNullOrEmpty(entityName) || string.IsNullOrEmpty(id)) return string.Empty;
            ApiParam apiParam = new ApiParam() { Keyword = id };

            switch (entityName)
            {
                case "t_search":
                    {
                        var r = await GetSearch(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_search_field":
                    {
                        var r = await GetSearchField(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_search_field_filter_field":
                    {
                        var r = await GetSearchFieldFilterField(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_chart":
                    {
                        var r = await GetChart(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_custom":
                    {
                        var r = await GetCustom(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_custom_btn":
                    {
                        var r = await GetCustomBtn(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_edit":
                    {
                        var r = await GetEdit(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_edit_field":
                    {
                        var r = await GetEditField(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_edit_field_filter_field":
                    {
                        var r = await GetEditFieldFilterField(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_edit_field_map":
                    {
                        var r = await GetEditFieldMap(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_edit_field_map_detail":
                    {
                        var r = await GetEditFieldMapDetail(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_edit_panel":
                    {
                        var r = await GetEditPanel(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_edit_btn":
                    {
                        var r = await GetEditBtn(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_table":
                    {
                        var r = await GetTable(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_table_field":
                    {
                        var r = await GetTableField(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_table_field_filter_field":
                    {
                        var r = await GetTableFieldFilterField(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_table_filter_field":
                    {
                        var r = await GetTableFilterField(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_table_panel":
                    {
                        var r = await GetTablePanel(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_table_btn":
                    {
                        var r = await GetTableBtn(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_tree":
                    {
                        var r = await GetTree(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_tree_field":
                    {
                        var r = await GetTreeField(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_tree_btn":
                    {
                        var r = await GetTreeBtn(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
                case "t_form":
                    {
                        var r = await GetForm(apiParam);
                        if (r != null) return r.ToJson();
                    }
                    break;
            }
            return string.Empty;
        }


        public async Task<bool> DeleteByParam(string entityName, string id)
        {
            if (string.IsNullOrEmpty(entityName) || string.IsNullOrEmpty(id)) return false;
            switch (entityName)
            {
                case "t_search":
                    {
                        return await DeleteSearch(id);

                    }
                case "t_search_field":
                    {
                        return await DeleteSearchField(id);

                    }

                case "t_search_field_filter_field":
                    {
                        return await DeleteSearchFieldFilterField(id); 
                    } 
                case "t_chart":
                    {
                        return await DeleteChart(id);

                    }
                case "t_custom":
                    {
                        return await DeleteCustom(id);

                    }
                case "t_custom_btn":
                    {
                        return await DeleteCustomBtn(id);

                    }
                case "t_edit":
                    {
                        return await DeleteEdit(id);

                    }
                case "t_edit_field":
                    {
                        return await DeleteEditField(id);

                    }
                case "t_edit_field_filter_field":
                    {
                        return await DeleteEditFieldFilterField(id);

                    }
                case "t_edit_field_map":
                    {
                        return await DeleteEditFieldMap(id);

                    }
                case "t_edit_field_map_detail":
                    {
                        return await DeleteEditFieldMapDetail(id);

                    }
                case "t_edit_panel":
                    {
                        return await DeleteEditPanel(id);

                    }
                case "t_edit_btn":
                    {
                        return await DeleteEditBtn(id);

                    }
                case "t_table":
                    {
                        LibRedisCacheUtils.Delete(Provider, $"statis_used_form_tables_{UserToken?.OrgId}");
                        return await DeleteTable(id);

                    }
                case "t_table_field":
                    {
                        return await DeleteTableField(id);

                    }
                case "t_table_field_filter_field":
                    {
                        return await DeleteTableFieldFilterField(id);

                    }
                case "t_table_filter_field":
                    {
                        return await DeleteTableFilterField(id);

                    }
                case "t_table_panel":
                    {
                        return await DeleteTablePanel(id);

                    }
                case "t_table_btn":
                    {
                        return await DeleteTableBtn(id);

                    }
                case "t_tree":
                    {
                        return await DeleteTree(id);

                    }
                case "t_tree_field":
                    {
                        return await DeleteTreeField(id);

                    }
                case "t_tree_btn":
                    {
                        return await DeleteTreeBtn(id);

                    }
                case "t_form":
                    {
                        LibRedisCacheUtils.Delete(Provider, $"statis_used_form_tables_{UserToken?.OrgId}");
                        LibRedisCacheUtils.Delete(Provider, $"fcp_form_info_{id}");
                        return await DeleteForm(id);
                    }
            }
            return false;
        }
        #region search

        private async Task<ApiPagedResult<t_search>>
                 OrderPageSearch(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_search>(apiOrderPageParam, null);
            if (r != null && r.Data != null)
                await EntityToWebModelUtls.GetSearchLabelsAsync(r.Data.ToList(), this.lang, this.fcpRepository.CommonRepository);
            return r;

        }


        private async Task<t_search> GetSearch(ApiParam apiParam)
        {
            var serach = await this.fcpRepository.CommonRepository.GetEntityAsync<t_search>(apiParam.Keyword);
            if (serach != null)
            {
                var serachs = new List<t_search>();
                serachs.Add(serach);
                await EntityToWebModelUtls.GetSearchLabelsAsync(serachs, this.lang, this.fcpRepository.CommonRepository);
            }
            return serach;

        }


        private async Task<bool> FullUpdateSearch(t_search serach)
        {
            List<t_search> addSerachs = new List<t_search>();
            List<t_search> updateSerachs = new List<t_search>();
            List<t_label> addLabels = new List<t_label>();
            List<t_label> updateLabels = new List<t_label>();
            List<t_label_detail> addLabelDetails = new List<t_label_detail>();
            List<t_label_detail> updateLabelDetails = new List<t_label_detail>();
            if (string.IsNullOrEmpty(serach.id))
            {
                this.SetEntityId(serach);
                addSerachs.Add(serach);
            }
            else
                updateSerachs.Add(serach);
            t_label label = null;
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            if (!string.IsNullOrEmpty(serach.label_label_id))
                label = await this.fcpRepository.CommonRepository.GetEntityAsync<t_label>(serach.label_label_id);
            if (label == null)
            {
                label = new t_label();
                label.org_id = serach.org_id;
                this.SetEntityId(label);
                addLabels.Add(label);
                serach.label_label_id = label.id;
            }
            else
            {
                labelDetails = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_label_detail>(new ApiParam() { BeginTime = label.create_time },
                        t => t.label_id == label.id)).ToList();
            }

            var ld = labelDetails.FirstOrDefault(t => string.IsNullOrEmpty(t.lang) || t.lang == this.lang);
            if (ld == null)
            {
                ld = new t_label_detail();
                ld.org_id = label.org_id;
                ld.label_id = label.id;
                this.SetEntityId(ld);
                addLabelDetails.Add(ld);
            }
            else
                updateLabelDetails.Add(ld);
            ld.label = serach.vir_label;
            ld.lang = this.lang;
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addSerachs);
                this.fcpRepository.CommonRepository.AddEntities(addLabels);
                this.fcpRepository.CommonRepository.AddEntities(addLabelDetails);

                this.fcpRepository.CommonRepository.UpdateEntities(updateSerachs);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabels);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabelDetails);
            });
            await _fcpService.FormConfigVersionAsync(serach.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteSearch(string id)
        {
            var serach = await this.fcpRepository.CommonRepository.GetEntityAsync<t_search>(id);

            var serachFieldIds = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_search_field>(t =>
                     t.search_id == serach.id)).Select(t => t.id).ToList();
            await _fcpService.FormConfigVersionAsync(serach.fcp_form_id);

            await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_search>(id);
                this.fcpRepository.CommonRepository.DeleteEntity<t_search_field>(serachFieldIds);
            });
            return true;

        }
        #endregion


        #region searchField

        private async Task<ApiPagedResult<t_search_field>>
                 OrderPageSearchField(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_search_field>(apiOrderPageParam, null);
            if (r != null && r.Data != null)
                await EntityToWebModelUtls.GetSearchFieldLabelsAsync(r.Data.ToList(), this.lang, this.fcpRepository.CommonRepository);
            return r;

        }


        private async Task<t_search_field> GetSearchField(ApiParam apiParam)
        {
            var serach = await this.fcpRepository.CommonRepository.GetEntityAsync<t_search_field>(apiParam.Keyword);
            if (serach != null)
            {
                var serachs = new List<t_search_field>();
                serachs.Add(serach);
                await EntityToWebModelUtls.GetSearchFieldLabelsAsync(serachs, this.lang, this.fcpRepository.CommonRepository);
            }
            return serach;

        }


        private async Task<bool> FullUpdateSearchField(t_search_field serachField)
        {
            List<t_search_field> addSerachs = new List<t_search_field>();
            List<t_search_field> updateSerachs = new List<t_search_field>();
            List<t_label> addLabels = new List<t_label>();
            List<t_label> updateLabels = new List<t_label>();
            List<t_label_detail> addLabelDetails = new List<t_label_detail>();
            List<t_label_detail> updateLabelDetails = new List<t_label_detail>();
            if (string.IsNullOrEmpty(serachField.id))
            {
                this.SetEntityId(serachField);
                addSerachs.Add(serachField);
            }
            else
                updateSerachs.Add(serachField);
            t_label label = null;
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            if (!string.IsNullOrEmpty(serachField.field_label_id))
                label = await this.fcpRepository.CommonRepository.GetEntityAsync<t_label>(serachField.field_label_id);
            if (label == null)
            {
                label = new t_label();
                label.org_id = serachField.org_id;
                this.SetEntityId(label);
                addLabels.Add(label);
                serachField.field_label_id = label.id;
            }
            else
            {
                labelDetails = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_label_detail>(new ApiParam() { BeginTime = label.create_time },
                        t => t.label_id == label.id)).ToList();
            }

            var ld = labelDetails.FirstOrDefault(t => string.IsNullOrEmpty(t.lang) || t.lang == this.lang);
            if (ld == null)
            {
                ld = new t_label_detail();
                ld.org_id = label.org_id;
                ld.label_id = label.id;
                this.SetEntityId(ld);
                addLabelDetails.Add(ld);
            }
            else
                updateLabelDetails.Add(ld);
            ld.label = serachField.vir_field_label;
            ld.lang = this.lang;
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addSerachs);
                this.fcpRepository.CommonRepository.AddEntities(addLabels);
                this.fcpRepository.CommonRepository.AddEntities(addLabelDetails);

                this.fcpRepository.CommonRepository.UpdateEntities(updateSerachs);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabels);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabelDetails);
            });
            await _fcpService.FormConfigVersionAsync(serachField.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteSearchField(string id)
        {
            var serachField = await this.fcpRepository.CommonRepository.GetEntityAsync<t_search_field>(id);
            await _fcpService.FormConfigVersionAsync(serachField.fcp_form_id);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_search_field>(id);
            });
            return true;

        }
        #endregion


        #region searchFieldFilterField

        private async Task<ApiPagedResult<t_search_field_filter_field>>
                 OrderPageSearchFieldFilterField(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_search_field_filter_field>(apiOrderPageParam, null);
            // await EntityToWebModelUtls.GetEditFieldLabelsAsync(r.Data.ToList(), apiOrderPageParam.Lang, this.fcpRepository.CommonRepository);;
            return r;

        }


        private async Task<t_search_field_filter_field> GetSearchFieldFilterField(ApiParam apiParam)
        {
            var edit = await this.fcpRepository.CommonRepository.GetEntityAsync<t_search_field_filter_field>(apiParam.Keyword);
            return edit;

        }


        private async Task<bool> FullUpdateSearchFieldFilterField(t_search_field_filter_field editField)
        {
            List<t_search_field_filter_field> addEdits = new List<t_search_field_filter_field>();
            List<t_search_field_filter_field> updateEdits = new List<t_search_field_filter_field>();
            if (string.IsNullOrEmpty(editField.id))
            {
                this.SetEntityId(editField);
                addEdits.Add(editField);
            }
            else
                updateEdits.Add(editField);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addEdits);

                this.fcpRepository.CommonRepository.UpdateEntities(updateEdits);
            });
            await _fcpService.FormConfigVersionAsync(editField.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteSearchFieldFilterField(string id)
        {
            var editField = await this.fcpRepository.CommonRepository.GetEntityAsync<t_search_field_filter_field>(id);
            await _fcpService.FormConfigVersionAsync(editField.fcp_form_id);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_search_field_filter_field>(id);
            });
            return true;

        }
        #endregion


        #region chart

        private async Task<ApiPagedResult<t_chart>>
                 OrderPageChart(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_chart>(apiOrderPageParam, null);
            if (r != null && r.Data != null)
                await EntityToWebModelUtls.GetChartLabelsAsync(r.Data.ToList(), this.lang, this.fcpRepository.CommonRepository); ;
            return r;

        }


        private async Task<t_chart> GetChart(ApiParam apiParam)
        {
            var chart = await this.fcpRepository.CommonRepository.GetEntityAsync<t_chart>(apiParam.Keyword);
            if (chart != null)
            {
                var charts = new List<t_chart>();
                charts.Add(chart);
                await EntityToWebModelUtls.GetChartLabelsAsync(charts, this.lang, this.fcpRepository.CommonRepository); ;
            }
            return chart;

        }


        private async Task<bool> FullUpdateChart(t_chart chart)
        {
            List<t_chart> addCharts = new List<t_chart>();
            List<t_chart> updateCharts = new List<t_chart>();
            List<t_label> addLabels = new List<t_label>();
            List<t_label> updateLabels = new List<t_label>();
            List<t_label_detail> addLabelDetails = new List<t_label_detail>();
            List<t_label_detail> updateLabelDetails = new List<t_label_detail>();
            if (string.IsNullOrEmpty(chart.id))
            {
                this.SetEntityId(chart);
                addCharts.Add(chart);
            }
            else
                updateCharts.Add(chart);
            t_label label = null;
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            if (!string.IsNullOrEmpty(chart.label_label_id))
                label = await this.fcpRepository.CommonRepository.GetEntityAsync<t_label>(chart.label_label_id);
            if (label == null)
            {
                label = new t_label();
                label.org_id = chart.org_id;
                this.SetEntityId(label);
                addLabels.Add(label);
                chart.label_label_id = label.id;
            }
            else
            {
                labelDetails = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_label_detail>(new ApiParam() { BeginTime = label.create_time },
                        t => t.label_id == label.id)).ToList();
            }

            var ld = labelDetails.FirstOrDefault(t => string.IsNullOrEmpty(t.lang) || t.lang == this.lang);
            if (ld == null)
            {
                ld = new t_label_detail();
                ld.org_id = label.org_id;
                ld.label_id = label.id;
                this.SetEntityId(ld);
                addLabelDetails.Add(ld);
            }
            else
                updateLabelDetails.Add(ld);
            ld.label = chart.vir_label;
            ld.lang = this.lang;
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addCharts);
                this.fcpRepository.CommonRepository.AddEntities(addLabels);
                this.fcpRepository.CommonRepository.AddEntities(addLabelDetails);

                this.fcpRepository.CommonRepository.UpdateEntities(updateCharts);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabels);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabelDetails);

            });
            await _fcpService.FormConfigVersionAsync(chart.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteChart(string id)
        {
            var chart = await this.fcpRepository.CommonRepository.GetEntityAsync<t_chart>(id);

            await _fcpService.FormConfigVersionAsync(chart.fcp_form_id);

             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_chart>(id);
            });
            return true;

        }
        #endregion




        #region custom

        private async Task<ApiPagedResult<t_custom>>
                 OrderPageCustom(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_custom>(apiOrderPageParam, null);
            if (r != null && r.Data != null) await EntityToWebModelUtls.GetCustomLabelsAsync(r.Data.ToList(), this.lang, this.fcpRepository.CommonRepository); ;
            return r;

        }


        private async Task<t_custom> GetCustom(ApiParam apiParam)
        {
            var custom = await this.fcpRepository.CommonRepository.GetEntityAsync<t_custom>(null, t => t.fcp_form_id == apiParam.Keyword);
            if (custom != null)
            {
                var customs = new List<t_custom>();
                customs.Add(custom);
                await EntityToWebModelUtls.GetCustomLabelsAsync(customs, this.lang, this.fcpRepository.CommonRepository); ;
            }
            return custom;

        }


        private async Task<bool> FullUpdateCustom(t_custom custom)
        {
            List<t_custom> addCustoms = new List<t_custom>();
            List<t_custom> updateCustoms = new List<t_custom>();
            List<t_label> addLabels = new List<t_label>();
            List<t_label> updateLabels = new List<t_label>();
            List<t_label_detail> addLabelDetails = new List<t_label_detail>();
            List<t_label_detail> updateLabelDetails = new List<t_label_detail>();
            if (string.IsNullOrEmpty(custom.id))
            {
                this.SetEntityId(custom);
                addCustoms.Add(custom);
            }
            else
                updateCustoms.Add(custom);
            t_label label = null;
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            if (!string.IsNullOrEmpty(custom.label_label_id))
                label = await this.fcpRepository.CommonRepository.GetEntityAsync<t_label>(custom.label_label_id);
            if (label == null)
            {
                label = new t_label();
                label.org_id = custom.org_id;
                this.SetEntityId(label);
                addLabels.Add(label);
                custom.label_label_id = label.id;
            }
            else
            {
                labelDetails = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_label_detail>(new ApiParam() { BeginTime = label.create_time },
                        t => t.label_id == label.id)).ToList();
            }

            var ld = labelDetails.FirstOrDefault(t => string.IsNullOrEmpty(t.lang) || t.lang == this.lang);
            if (ld == null)
            {
                ld = new t_label_detail();
                ld.org_id = label.org_id;
                ld.label_id = label.id;
                this.SetEntityId(ld);
                addLabelDetails.Add(ld);
            }
            else
                updateLabelDetails.Add(ld);
            ld.label = custom.vir_label;
            ld.lang = this.lang;
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addCustoms);
                this.fcpRepository.CommonRepository.AddEntities(addLabels);
                this.fcpRepository.CommonRepository.AddEntities(addLabelDetails);

                this.fcpRepository.CommonRepository.UpdateEntities(updateCustoms);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabels);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabelDetails);
            });
            await _fcpService.FormConfigVersionAsync(custom.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteCustom(string id)
        {
            var custom = await this.fcpRepository.CommonRepository.GetEntityAsync<t_custom>(id);

            var customBtnIds = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_custom_btn>(t =>
                 t.custom_id == custom.id)).Select(t => t.id).ToList();
            await _fcpService.FormConfigVersionAsync(custom.fcp_form_id);

             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_custom>(id);
                this.fcpRepository.CommonRepository.DeleteEntity<t_custom_btn>(customBtnIds);
            });
            return true;

        }
        #endregion




        #region customBtn

        private async Task<ApiPagedResult<t_custom_btn>>
                 OrderPageCustomBtn(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_custom_btn>(apiOrderPageParam, null);
            if (r != null && r.Data != null) await EntityToWebModelUtls.GetCustomBtnLabelsAsync(r.Data.ToList(), this.lang, this.fcpRepository.CommonRepository); ;
            return r;

        }


        private async Task<t_custom_btn> GetCustomBtn(ApiParam apiParam)
        {
            var custom = await this.fcpRepository.CommonRepository.GetEntityAsync<t_custom_btn>(apiParam.Keyword);
            if (custom != null)
            {
                var customs = new List<t_custom_btn>();
                customs.Add(custom);
                await EntityToWebModelUtls.GetCustomBtnLabelsAsync(customs, this.lang, this.fcpRepository.CommonRepository); ;
            }
            return custom;

        }


        private async Task<bool> FullUpdateCustomBtn(t_custom_btn customField)
        {
            List<t_custom_btn> addCustoms = new List<t_custom_btn>();
            List<t_custom_btn> updateCustoms = new List<t_custom_btn>();
            List<t_label> addLabels = new List<t_label>();
            List<t_label> updateLabels = new List<t_label>();
            List<t_label_detail> addLabelDetails = new List<t_label_detail>();
            List<t_label_detail> updateLabelDetails = new List<t_label_detail>();
            if (string.IsNullOrEmpty(customField.id))
            {
                this.SetEntityId(customField);
                addCustoms.Add(customField);
            }
            else
                updateCustoms.Add(customField);
            t_label label = null;
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            if (!string.IsNullOrEmpty(customField.btn_label_id))
                label = await this.fcpRepository.CommonRepository.GetEntityAsync<t_label>(customField.btn_label_id);
            if (label == null)
            {
                label = new t_label();
                label.org_id = customField.org_id;
                this.SetEntityId(label);
                addLabels.Add(label);
                customField.btn_label_id = label.id;
            }
            else
            {
                labelDetails = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_label_detail>(new ApiParam() { BeginTime = label.create_time },
                        t => t.label_id == label.id)).ToList();
            }

            var ld = labelDetails.FirstOrDefault(t => string.IsNullOrEmpty(t.lang) || t.lang == this.lang);
            if (ld == null)
            {
                ld = new t_label_detail();
                ld.org_id = label.org_id;
                ld.label_id = label.id;
                this.SetEntityId(ld);
                addLabelDetails.Add(ld);
            }
            else
                updateLabelDetails.Add(ld);
            ld.label = customField.vir_btn_label;
            ld.lang = this.lang;
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addCustoms);
                this.fcpRepository.CommonRepository.AddEntities(addLabels);
                this.fcpRepository.CommonRepository.AddEntities(addLabelDetails);

                this.fcpRepository.CommonRepository.UpdateEntities(updateCustoms);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabels);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabelDetails);
            });
            await _fcpService.FormConfigVersionAsync(customField.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteCustomBtn(string id)
        {
            var customPanel = await this.fcpRepository.CommonRepository.GetEntityAsync<t_custom_btn>(id);
            await _fcpService.FormConfigVersionAsync(customPanel.fcp_form_id);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_custom_btn>(id);
            });
            return true;

        }
        #endregion



        #region edit

        private async Task<ApiPagedResult<t_edit>>
                 OrderPageEdit(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_edit>(apiOrderPageParam, null);
            if (r != null && r.Data != null) await EntityToWebModelUtls.GetEditLabelsAsync(r.Data.ToList(), this.lang, this.fcpRepository.CommonRepository); ;
            return r;

        }


        private async Task<t_edit> GetEdit(ApiParam apiParam)
        {
            var edit = await this.fcpRepository.CommonRepository.GetEntityAsync<t_edit>(apiParam.Keyword);
            if (edit != null)
            {
                var edits = new List<t_edit>();
                edits.Add(edit);
                await EntityToWebModelUtls.GetEditLabelsAsync(edits, this.lang, this.fcpRepository.CommonRepository); ;
            }
            return edit;

        }


        private async Task<bool> FullUpdateEdit(t_edit edit)
        {
            List<t_edit> addEdits = new List<t_edit>();
            List<t_edit> updateEdits = new List<t_edit>();
            List<t_label> addLabels = new List<t_label>();
            List<t_label> updateLabels = new List<t_label>();
            List<t_label_detail> addLabelDetails = new List<t_label_detail>();
            List<t_label_detail> updateLabelDetails = new List<t_label_detail>();
            if (string.IsNullOrEmpty(edit.id))
            {
                this.SetEntityId(edit);
                addEdits.Add(edit);
            }
            else
                updateEdits.Add(edit);
            t_label label = null;
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            if (!string.IsNullOrEmpty(edit.label_label_id))
                label = await this.fcpRepository.CommonRepository.GetEntityAsync<t_label>(edit.label_label_id);
            if (label == null)
            {
                label = new t_label();
                label.org_id = edit.org_id;
                this.SetEntityId(label);
                addLabels.Add(label);
                edit.label_label_id = label.id;
            }
            else
            {
                labelDetails = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_label_detail>(new ApiParam() { BeginTime = label.create_time },
                        t => t.label_id == label.id)).ToList();
            }

            var ld = labelDetails.FirstOrDefault(t => string.IsNullOrEmpty(t.lang) || t.lang == this.lang);
            if (ld == null)
            {
                ld = new t_label_detail();
                ld.org_id = label.org_id;
                ld.label_id = label.id;
                this.SetEntityId(ld);
                addLabelDetails.Add(ld);
            }
            else
                updateLabelDetails.Add(ld);
            ld.label = edit.vir_label;
            ld.lang = this.lang;
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addEdits);
                this.fcpRepository.CommonRepository.AddEntities(addLabels);
                this.fcpRepository.CommonRepository.AddEntities(addLabelDetails);

                this.fcpRepository.CommonRepository.UpdateEntities(updateEdits);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabels);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabelDetails);
            });
            await _fcpService.FormConfigVersionAsync(edit.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteEdit(string id)
        {
            var edit = await this.fcpRepository.CommonRepository.GetEntityAsync<t_edit>(id);

            var editFieldIds = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_edit_field>(t =>
                     t.edit_id == edit.id)).Select(t => t.id).ToList();
            var editPanelIds = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_edit_panel>(t =>
                   t.edit_id == edit.id)).Select(t => t.id).ToList();
            var editBtnIds = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_edit_btn>(t =>
                 t.edit_id == edit.id)).Select(t => t.id).ToList();
            var editFieldFilterFieldIds = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_edit_field_filter_field>(t =>
                  t.edit_id == edit.id)).Select(t => t.id).ToList();
            var editFieldFilterGroupIds = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_edit_field_filter_group>(t =>
                 t.edit_id == edit.id)).Select(t => t.id).ToList();
            await _fcpService.FormConfigVersionAsync(edit.fcp_form_id);

             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_edit>(id);
                this.fcpRepository.CommonRepository.DeleteEntity<t_edit_field_filter_field>(editFieldFilterFieldIds);
                this.fcpRepository.CommonRepository.DeleteEntity<t_edit_field_filter_group>(editFieldFilterGroupIds);
                this.fcpRepository.CommonRepository.DeleteEntity<t_edit_field>(editFieldIds);
                this.fcpRepository.CommonRepository.DeleteEntity<t_edit_panel>(editPanelIds);
                this.fcpRepository.CommonRepository.DeleteEntity<t_edit_btn>(editBtnIds);
            });
            return true;

        }
        #endregion


        #region editField

        private async Task<ApiPagedResult<t_edit_field>>
                 OrderPageEditField(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_edit_field>(apiOrderPageParam, null);
            if (r != null && r.Data != null) await EntityToWebModelUtls.GetEditFieldLabelsAsync(r.Data.ToList(), this.lang, this.fcpRepository.CommonRepository); ;
            return r;

        }


        private async Task<t_edit_field> GetEditField(ApiParam apiParam)
        {
            var edit = await this.fcpRepository.CommonRepository.GetEntityAsync<t_edit_field>(apiParam.Keyword);
            if (edit != null)
            {
                var edits = new List<t_edit_field>();
                edits.Add(edit);
                await EntityToWebModelUtls.GetEditFieldLabelsAsync(edits, this.lang, this.fcpRepository.CommonRepository); ;
            }
            return edit;

        }


        private async Task<bool> FullUpdateEditField(t_edit_field editField)
        {
            List<t_edit_field> addEdits = new List<t_edit_field>();
            List<t_edit_field> updateEdits = new List<t_edit_field>();
            List<t_label> addLabels = new List<t_label>();
            List<t_label> updateLabels = new List<t_label>();
            List<t_label_detail> addLabelDetails = new List<t_label_detail>();
            List<t_label_detail> updateLabelDetails = new List<t_label_detail>();
            if (string.IsNullOrEmpty(editField.id))
            {
                this.SetEntityId(editField);
                addEdits.Add(editField);
            }
            else
                updateEdits.Add(editField);
            t_label label = null;
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            if (!string.IsNullOrEmpty(editField.field_label_id))
                label = await this.fcpRepository.CommonRepository.GetEntityAsync<t_label>(editField.field_label_id);
            if (label == null)
            {
                label = new t_label();
                label.org_id = editField.org_id;
                this.SetEntityId(label);
                addLabels.Add(label);
                editField.field_label_id = label.id;
            }
            else
            {
                labelDetails = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_label_detail>(new ApiParam() { BeginTime = label.create_time },
                        t => t.label_id == label.id)).ToList();
            }

            var ld = labelDetails.FirstOrDefault(t => string.IsNullOrEmpty(t.lang) || t.lang == this.lang);
            if (ld == null)
            {
                ld = new t_label_detail();
                ld.org_id = label.org_id;
                ld.label_id = label.id;
                this.SetEntityId(ld);
                addLabelDetails.Add(ld);
            }
            else
                updateLabelDetails.Add(ld);
            ld.label = editField.vir_field_label;
            ld.lang = this.lang;
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addEdits);
                this.fcpRepository.CommonRepository.AddEntities(addLabels);
                this.fcpRepository.CommonRepository.AddEntities(addLabelDetails);

                this.fcpRepository.CommonRepository.UpdateEntities(updateEdits);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabels);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabelDetails);
            });
            await _fcpService.FormConfigVersionAsync(editField.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteEditField(string id)
        {
            var editField = await this.fcpRepository.CommonRepository.GetEntityAsync<t_edit_field>(id);

            var editFieldFilterFieldIds = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_edit_field_filter_field>(t =>
                t.edit_field_id == editField.id)).Select(t => t.id).ToList();
            var editFieldFilterGroupIds = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_edit_field_filter_group>(t =>
                 t.edit_field_id == editField.id)).Select(t => t.id).ToList();
            await _fcpService.FormConfigVersionAsync(editField.fcp_form_id);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_edit_field>(id);
                this.fcpRepository.CommonRepository.DeleteEntity<t_edit_field_filter_field>(editFieldFilterFieldIds);
                this.fcpRepository.CommonRepository.DeleteEntity<t_edit_field_filter_group>(editFieldFilterGroupIds);
            });
            return true;

        }
        #endregion


        #region editFieldFilterField

        private async Task<ApiPagedResult<t_edit_field_filter_field>>
                 OrderPageEditFieldFilterField(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_edit_field_filter_field>(apiOrderPageParam, null);
            // await EntityToWebModelUtls.GetEditFieldLabelsAsync(r.Data.ToList(), apiOrderPageParam.Lang, this.fcpRepository.CommonRepository);;
            return r;

        }


        private async Task<t_edit_field_filter_field> GetEditFieldFilterField(ApiParam apiParam)
        {
            var edit = await this.fcpRepository.CommonRepository.GetEntityAsync<t_edit_field_filter_field>(apiParam.Keyword);
            return edit;

        }


        private async Task<bool> FullUpdateEditFieldFilterField(t_edit_field_filter_field editField)
        {
            List<t_edit_field_filter_field> addEdits = new List<t_edit_field_filter_field>();
            List<t_edit_field_filter_field> updateEdits = new List<t_edit_field_filter_field>();
            if (string.IsNullOrEmpty(editField.id))
            {
                this.SetEntityId(editField);
                addEdits.Add(editField);
            }
            else
                updateEdits.Add(editField);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addEdits);

                this.fcpRepository.CommonRepository.UpdateEntities(updateEdits);
            });
            await _fcpService.FormConfigVersionAsync(editField.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteEditFieldFilterField(string id)
        {
            var editField = await this.fcpRepository.CommonRepository.GetEntityAsync<t_edit_field_filter_field>(id);
            await _fcpService.FormConfigVersionAsync(editField.fcp_form_id);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_edit_field_filter_field>(id);
            });
            return true;

        }
        #endregion



        #region editFieldMap

        private async Task<ApiPagedResult<t_edit_field_map>>
                 OrderPageEditFieldMap(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_edit_field_map>(apiOrderPageParam, null);
            // await EntityToWebModelUtls.GetEditFieldLabelsAsync(r.Data.ToList(), apiOrderPageParam.Lang, this.fcpRepository.CommonRepository);;
            return r;

        }


        private async Task<t_edit_field_map> GetEditFieldMap(ApiParam apiParam)
        {
            var edit = await this.fcpRepository.CommonRepository.GetEntityAsync<t_edit_field_map>(apiParam.Keyword);
            return edit;

        }


        private async Task<bool> FullUpdateEditFieldMap(t_edit_field_map editField)
        {
            List<t_edit_field_map> addEdits = new List<t_edit_field_map>();
            List<t_edit_field_map> updateEdits = new List<t_edit_field_map>();
            if (string.IsNullOrEmpty(editField.id))
            {
                this.SetEntityId(editField);
                addEdits.Add(editField);
            }
            else
                updateEdits.Add(editField);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addEdits);

                this.fcpRepository.CommonRepository.UpdateEntities(updateEdits);
            });
            await _fcpService.FormConfigVersionAsync(editField.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteEditFieldMap(string id)
        {
            var editField = await this.fcpRepository.CommonRepository.GetEntityAsync<t_edit_field_map>(id);
            var editFieldDetail = await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_edit_field_map_detail>(null, t => t.edit_field_map_id == id);
            var editFieldDetailIds = editFieldDetail.Select(t => t.id).ToList();
            await _fcpService.FormConfigVersionAsync(editField.fcp_form_id);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_edit_field_map>(id);
                this.fcpRepository.CommonRepository.DeleteEntity<t_edit_field_map_detail>(editFieldDetailIds);
            });
            return true;

        }
        #endregion

        #region editFieldMapDetail

        private async Task<ApiPagedResult<t_edit_field_map_detail>>
                 OrderPageEditFieldMapDetail(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_edit_field_map_detail>(apiOrderPageParam, null);
            // await EntityToWebModelUtls.GetEditFieldLabelsAsync(r.Data.ToList(), apiOrderPageParam.Lang, this.fcpRepository.CommonRepository);;
            return r;

        }


        private async Task<t_edit_field_map_detail> GetEditFieldMapDetail(ApiParam apiParam)
        {
            var edit = await this.fcpRepository.CommonRepository.GetEntityAsync<t_edit_field_map_detail>(apiParam.Keyword);
            return edit;

        }


        private async Task<bool> FullUpdateEditFieldMapDetail(t_edit_field_map_detail editField)
        {
            List<t_edit_field_map_detail> addEdits = new List<t_edit_field_map_detail>();
            List<t_edit_field_map_detail> updateEdits = new List<t_edit_field_map_detail>();
            if (string.IsNullOrEmpty(editField.id))
            {
                this.SetEntityId(editField);
                addEdits.Add(editField);
            }
            else
                updateEdits.Add(editField);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addEdits);

                this.fcpRepository.CommonRepository.UpdateEntities(updateEdits);
            });
            await _fcpService.FormConfigVersionAsync(editField.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteEditFieldMapDetail(string id)
        {
            var editField = await this.fcpRepository.CommonRepository.GetEntityAsync<t_edit_field_map_detail>(id);

            await _fcpService.FormConfigVersionAsync(editField.fcp_form_id);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_edit_field_map_detail>(id);
            });
            return true;

        }
        #endregion

        #region editPanel

        private async Task<ApiPagedResult<t_edit_panel>>
                 OrderPageEditPanel(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_edit_panel>(apiOrderPageParam, null);
            if (r != null && r.Data != null) await EntityToWebModelUtls.GetEditPanelLabelsAsync(r.Data.ToList(), this.lang, this.fcpRepository.CommonRepository); ;
            return r;

        }


        private async Task<t_edit_panel> GetEditPanel(ApiParam apiParam)
        {
            var edit = await this.fcpRepository.CommonRepository.GetEntityAsync<t_edit_panel>(apiParam.Keyword);
            if (edit != null)
            {
                var edits = new List<t_edit_panel>();
                edits.Add(edit);
                await EntityToWebModelUtls.GetEditPanelLabelsAsync(edits, this.lang, this.fcpRepository.CommonRepository); ;
            }
            return edit;

        }


        private async Task<bool> FullUpdateEditPanel(t_edit_panel editField)
        {
            List<t_edit_panel> addEdits = new List<t_edit_panel>();
            List<t_edit_panel> updateEdits = new List<t_edit_panel>();
            List<t_label> addLabels = new List<t_label>();
            List<t_label> updateLabels = new List<t_label>();
            List<t_label_detail> addLabelDetails = new List<t_label_detail>();
            List<t_label_detail> updateLabelDetails = new List<t_label_detail>();
            if (string.IsNullOrEmpty(editField.id))
            {
                this.SetEntityId(editField);
                addEdits.Add(editField);
            }
            else
                updateEdits.Add(editField);
            t_label label = null;
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            if (!string.IsNullOrEmpty(editField.label_label_id))
                label = await this.fcpRepository.CommonRepository.GetEntityAsync<t_label>(editField.label_label_id);
            if (label == null)
            {
                label = new t_label();
                label.org_id = editField.org_id;
                this.SetEntityId(label);
                addLabels.Add(label);
                editField.label_label_id = label.id;
            }
            else
            {
                labelDetails = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_label_detail>(new ApiParam() { BeginTime = label.create_time },
                        t => t.label_id == label.id)).ToList();
            }

            var ld = labelDetails.FirstOrDefault(t => string.IsNullOrEmpty(t.lang) || t.lang == this.lang);
            if (ld == null)
            {
                ld = new t_label_detail();
                ld.org_id = label.org_id;
                ld.label_id = label.id;
                this.SetEntityId(ld);
                addLabelDetails.Add(ld);
            }
            else
                updateLabelDetails.Add(ld);
            ld.label = editField.vir_label;
            ld.lang = this.lang;
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addEdits);
                this.fcpRepository.CommonRepository.AddEntities(addLabels);
                this.fcpRepository.CommonRepository.AddEntities(addLabelDetails);

                this.fcpRepository.CommonRepository.UpdateEntities(updateEdits);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabels);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabelDetails);
            });
            await _fcpService.FormConfigVersionAsync(editField.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteEditPanel(string id)
        {
            var editPanel = await this.fcpRepository.CommonRepository.GetEntityAsync<t_edit_panel>(id);
            var editFields = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_edit_field>(t =>
                t.edit_panel_id == editPanel.id)).ToList();
            editFields.ForEach(t => t.edit_panel_id = null);
            await _fcpService.FormConfigVersionAsync(editPanel.fcp_form_id);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_edit_panel>(id);
                this.fcpRepository.CommonRepository.UpdateEntities(editFields);
            });
            return true;

        }
        #endregion




        #region editBtn

        private async Task<ApiPagedResult<t_edit_btn>>
                 OrderPageEditBtn(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_edit_btn>(apiOrderPageParam, null);
            if (r != null && r.Data != null) await EntityToWebModelUtls.GetEditBtnLabelsAsync(r.Data.ToList(), this.lang, this.fcpRepository.CommonRepository); ;
            return r;

        }


        private async Task<t_edit_btn> GetEditBtn(ApiParam apiParam)
        {
            var edit = await this.fcpRepository.CommonRepository.GetEntityAsync<t_edit_btn>(apiParam.Keyword);
            if (edit != null)
            {
                var edits = new List<t_edit_btn>();
                edits.Add(edit);
                await EntityToWebModelUtls.GetEditBtnLabelsAsync(edits, this.lang, this.fcpRepository.CommonRepository); ;
            }
            return edit;

        }


        private async Task<bool> FullUpdateEditBtn(t_edit_btn editField)
        {
            List<t_edit_btn> addEdits = new List<t_edit_btn>();
            List<t_edit_btn> updateEdits = new List<t_edit_btn>();
            List<t_label> addLabels = new List<t_label>();
            List<t_label> updateLabels = new List<t_label>();
            List<t_label_detail> addLabelDetails = new List<t_label_detail>();
            List<t_label_detail> updateLabelDetails = new List<t_label_detail>();
            if (string.IsNullOrEmpty(editField.id))
            {
                this.SetEntityId(editField);
                addEdits.Add(editField);
            }
            else
                updateEdits.Add(editField);
            t_label label = null;
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            if (!string.IsNullOrEmpty(editField.btn_label_id))
                label = await this.fcpRepository.CommonRepository.GetEntityAsync<t_label>(editField.btn_label_id);
            if (label == null)
            {
                label = new t_label();
                label.org_id = editField.org_id;
                this.SetEntityId(label);
                addLabels.Add(label);
                editField.btn_label_id = label.id;
            }
            else
            {
                labelDetails = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_label_detail>(new ApiParam() { BeginTime = label.create_time },
                        t => t.label_id == label.id)).ToList();
            }

            var ld = labelDetails.FirstOrDefault(t => string.IsNullOrEmpty(t.lang) || t.lang == this.lang);
            if (ld == null)
            {
                ld = new t_label_detail();
                ld.org_id = label.org_id;
                ld.label_id = label.id;
                this.SetEntityId(ld);
                addLabelDetails.Add(ld);
            }
            else
                updateLabelDetails.Add(ld);
            ld.label = editField.vir_btn_label;
            ld.lang = this.lang;
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addEdits);
                this.fcpRepository.CommonRepository.AddEntities(addLabels);
                this.fcpRepository.CommonRepository.AddEntities(addLabelDetails);

                this.fcpRepository.CommonRepository.UpdateEntities(updateEdits);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabels);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabelDetails);
            });
            await _fcpService.FormConfigVersionAsync(editField.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteEditBtn(string id)
        {
            var editPanel = await this.fcpRepository.CommonRepository.GetEntityAsync<t_edit_btn>(id);
            await _fcpService.FormConfigVersionAsync(editPanel.fcp_form_id);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_edit_btn>(id);
            });
            return true;

        }
        #endregion


        private async Task<ApiPagedResult<t_form>>
                 OrderPageForm(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_form>(apiOrderPageParam, null);
              //await EntityToWebModelUtls.GetFormLabelsAsync(r.Data.ToList(), apiOrderPageParam.Lang, this.fcpRepository.CommonRepository);;
            return r;

        }


        private async Task<t_form> GetForm(ApiParam apiParam)
        {
            var custom = await this.fcpRepository.CommonRepository.GetEntityAsync<t_form>(apiParam.Keyword);
            if (custom != null)
            {
                var customs = new List<t_form>();
                customs.Add(custom);
               // await EntityToWebModelUtls.GetFormLabelsAsync(customs, apiParam.Lang, this.fcpRepository.CommonRepository); ;
            }
            return custom;

        }


        private async Task<bool> FullUpdateForm(t_form form)
        {
            List<t_form> saveCustoms = new List<t_form>();
            saveCustoms.Add(form);
            var isAdd = string.IsNullOrEmpty(form.id);
            if (string.IsNullOrEmpty(form.id))
                this.SetEntityId(form);

            var dbForm = await this.fcpRepository.CommonRepository.GetEntityAsync<t_form>(null, t => t.code == form.code && t.org_id == form.org_id);
            if (dbForm != null && dbForm.id != form.id)
                this.ThrowError("02100001", dbForm.code, dbForm.name);

            if (isAdd)
            {
                var dbForm2 = await this.fcpRepository.CommonRepository.GetEntityAsync<t_form>(null, t => t.code == form.code);
                if (dbForm2 != null)
                    this.ThrowError("02100001", dbForm2.code, dbForm2.name);

                ////var prefix = form.code.Substring(0, 2);
                ////var dbForm2 = await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_form>(null, t => t.code.Contains(prefix));
                ////if (dbForm2.Any())
                ////{
                ////    var pp = dbForm2.Select(t => LibSysUtils.ToInt(t.code.Substring(2))).Max(t => t);
                ////    var pp2 = LibSysUtils.ToInt(form.code.Substring(2));
                ////    if (pp > pp2)
                ////    {
                ////        this.ThrowError("02100004", prefix, prefix + pp);
                ////    }
                ////}
            }

             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.SaveEntities(saveCustoms); 
            });
            await _fcpService.FormConfigVersionAsync(form.id);
            return true;

        }


        private async Task<bool> DeleteForm(string id)
        {
            if (!string.IsNullOrEmpty(id))
                LibMessageUtils.ThrowError(this.Provider, "02100000");
            var customPanel = await this.fcpRepository.CommonRepository.GetEntityAsync<t_custom_btn>(id);
            await _fcpService.FormConfigVersionAsync(customPanel.fcp_form_id);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_custom_btn>(id);
            });
            return true;

        }


        #region table

        private async Task<ApiPagedResult<t_table>>
                 OrderPageTable(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_table>(apiOrderPageParam, null);
            if (r != null && r.Data != null) await EntityToWebModelUtls.GetTableLabelsAsync(r.Data.ToList(), this.lang, this.fcpRepository.CommonRepository);
            return r;

        }


        private async Task<t_table> GetTable(ApiParam apiParam)
        {
            var table = await this.fcpRepository.CommonRepository.GetEntityAsync<t_table>(apiParam.Keyword);
            if (table != null)
            {
                var tables = new List<t_table>();
                tables.Add(table);
                await EntityToWebModelUtls.GetTableLabelsAsync(tables, this.lang, this.fcpRepository.CommonRepository);
            }
            return table;

        }


        private async Task<bool> FullUpdateTable(t_table table)
        {
            List<t_table> addTables = new List<t_table>();
            List<t_table> updateTables = new List<t_table>();
            List<t_label> addLabels = new List<t_label>();
            List<t_label> updateLabels = new List<t_label>();
            List<t_label_detail> addLabelDetails = new List<t_label_detail>();
            List<t_label_detail> updateLabelDetails = new List<t_label_detail>();
            if (string.IsNullOrEmpty(table.id))
            {
                this.SetEntityId(table);
                addTables.Add(table);
            }
            else
                updateTables.Add(table);
            t_label label = null;
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            if (!string.IsNullOrEmpty(table.label_label_id))
                label = await this.fcpRepository.CommonRepository.GetEntityAsync<t_label>(table.label_label_id);
            if (label == null)
            {
                label = new t_label();
                label.org_id = table.org_id;
                this.SetEntityId(label);
                addLabels.Add(label);
                table.label_label_id = label.id;
            }
            else
            {
                labelDetails = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_label_detail>(new ApiParam() { BeginTime = label.create_time },
                        t => t.label_id == label.id)).ToList();
            }

            var ld = labelDetails.FirstOrDefault(t => string.IsNullOrEmpty(t.lang) || t.lang == this.lang);
            if (ld == null)
            {
                ld = new t_label_detail();
                ld.org_id = label.org_id;
                ld.label_id = label.id;
                this.SetEntityId(ld);
                addLabelDetails.Add(ld);
            }
            else
                updateLabelDetails.Add(ld);
            ld.label = table.vir_label;
            ld.lang = this.lang;
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addTables);
                this.fcpRepository.CommonRepository.AddEntities(addLabels);
                this.fcpRepository.CommonRepository.AddEntities(addLabelDetails);

                this.fcpRepository.CommonRepository.UpdateEntities(updateTables);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabels);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabelDetails);
            });
            await _fcpService.FormConfigVersionAsync(table.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteTable(string id)
        {
            var table = await this.fcpRepository.CommonRepository.GetEntityAsync<t_table>(id);

            var tableFieldIds = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_table_field>(t =>
                     t.table_id == table.id)).Select(t => t.id).ToList();
            var tableFilterFieldIds = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_table_filter_field>(t =>
                    t.table_id == table.id)).Select(t => t.id).ToList();
            var tableFilterGroupIds = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_table_filter_group>(t =>
                  t.table_id == table.id)).Select(t => t.id).ToList();
            var tablePanelIds = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_table_panel>(t =>
                   t.table_id == table.id)).Select(t => t.id).ToList();
            var tableBtnIds = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_table_btn>(t =>
                 t.table_id == table.id)).Select(t => t.id).ToList();
            var tableFieldFilterFieldIds = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_table_field_filter_field>(t =>
                  t.table_id == table.id)).Select(t => t.id).ToList();
            var tableFieldFilterGroupIds = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_table_field_filter_group>(t =>
                 t.table_id == table.id)).Select(t => t.id).ToList();
            await _fcpService.FormConfigVersionAsync(table.fcp_form_id);

             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_table>(id);
                this.fcpRepository.CommonRepository.DeleteEntity<t_table_field_filter_field>(tableFieldFilterFieldIds);
                this.fcpRepository.CommonRepository.DeleteEntity<t_table_field_filter_group>(tableFieldFilterGroupIds);
                this.fcpRepository.CommonRepository.DeleteEntity<t_table_filter_field>(tableFilterFieldIds);
                this.fcpRepository.CommonRepository.DeleteEntity<t_table_filter_group>(tableFilterGroupIds);
                this.fcpRepository.CommonRepository.DeleteEntity<t_table_field>(tableFieldIds);
                this.fcpRepository.CommonRepository.DeleteEntity<t_table_panel>(tablePanelIds);
                this.fcpRepository.CommonRepository.DeleteEntity<t_table_btn>(tableBtnIds);
            });
            return true;

        }
        #endregion


        #region tableField

        private async Task<ApiPagedResult<t_table_field>>
                 OrderPageTableField(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_table_field>(apiOrderPageParam, null);
            if (r != null && r.Data != null) await EntityToWebModelUtls.GetTableFieldLabelsAsync(r.Data.ToList(), this.lang, this.fcpRepository.CommonRepository);
            return r;

        }


        private async Task<t_table_field> GetTableField(ApiParam apiParam)
        {
            var table = await this.fcpRepository.CommonRepository.GetEntityAsync<t_table_field>(apiParam.Keyword);
            if (table != null)
            {
                var tables = new List<t_table_field>();
                tables.Add(table);
                await EntityToWebModelUtls.GetTableFieldLabelsAsync(tables, this.lang, this.fcpRepository.CommonRepository);
            }
            return table;

        }


        private async Task<bool> FullUpdateTableField(t_table_field tableField)
        {
            List<t_table_field> addTables = new List<t_table_field>();
            List<t_table_field> updateTables = new List<t_table_field>();
            List<t_label> addLabels = new List<t_label>();
            List<t_label> updateLabels = new List<t_label>();
            List<t_label_detail> addLabelDetails = new List<t_label_detail>();
            List<t_label_detail> updateLabelDetails = new List<t_label_detail>();
            if (string.IsNullOrEmpty(tableField.id))
            {
                this.SetEntityId(tableField);
                addTables.Add(tableField);
            }
            else
                updateTables.Add(tableField);
            t_label label = null;
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            if (!string.IsNullOrEmpty(tableField.field_label_id))
                label = await this.fcpRepository.CommonRepository.GetEntityAsync<t_label>(tableField.field_label_id);
            if (label == null)
            {
                label = new t_label();
                label.org_id = tableField.org_id;
                this.SetEntityId(label);
                addLabels.Add(label);
                tableField.field_label_id = label.id;
            }
            else
            {
                labelDetails = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_label_detail>(new ApiParam() { BeginTime = label.create_time },
                        t => t.label_id == label.id)).ToList();
            }

            var ld = labelDetails.FirstOrDefault(t => string.IsNullOrEmpty(t.lang) || t.lang == this.lang);
            if (ld == null)
            {
                ld = new t_label_detail();
                ld.org_id = label.org_id;
                ld.label_id = label.id;
                this.SetEntityId(ld);
                addLabelDetails.Add(ld);
            }
            else
                updateLabelDetails.Add(ld);
            ld.label = tableField.vir_field_label;
            ld.lang = this.lang;
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addTables);
                this.fcpRepository.CommonRepository.AddEntities(addLabels);
                this.fcpRepository.CommonRepository.AddEntities(addLabelDetails);

                this.fcpRepository.CommonRepository.UpdateEntities(updateTables);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabels);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabelDetails);
            });
            await _fcpService.FormConfigVersionAsync(tableField.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteTableField(string id)
        {
            var tableField = await this.fcpRepository.CommonRepository.GetEntityAsync<t_table_field>(id);

            var tableFieldFilterFieldIds = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_table_field_filter_field>(t =>
                t.table_field_id == tableField.id)).Select(t => t.id).ToList();
            var tableFieldFilterGroupIds = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_table_field_filter_group>(t =>
                 t.table_field_id == tableField.id)).Select(t => t.id).ToList();
            await _fcpService.FormConfigVersionAsync(tableField.fcp_form_id);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_table_field>(id);
                this.fcpRepository.CommonRepository.DeleteEntity<t_table_field_filter_field>(tableFieldFilterFieldIds);
                this.fcpRepository.CommonRepository.DeleteEntity<t_table_field_filter_group>(tableFieldFilterGroupIds);
            });
            return true;

        }
        #endregion


        #region tableFieldFilterField

        private async Task<ApiPagedResult<t_table_field_filter_field>>
                 OrderPageTableFieldFilterField(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_table_field_filter_field>(apiOrderPageParam, null);
            // await EntityToWebModelUtls.GetTableFieldLabelsAsync(r.Data.ToList(), apiOrderPageParam.Lang, this.fcpRepository.CommonRepository);
            return r;

        }


        private async Task<t_table_field_filter_field> GetTableFieldFilterField(ApiParam apiParam)
        {
            var table = await this.fcpRepository.CommonRepository.GetEntityAsync<t_table_field_filter_field>(apiParam.Keyword);
            return table;

        }


        private async Task<bool> FullUpdateTableFieldFilterField(t_table_field_filter_field tableField)
        {
            List<t_table_field_filter_field> addTables = new List<t_table_field_filter_field>();
            List<t_table_field_filter_field> updateTables = new List<t_table_field_filter_field>();
            if (string.IsNullOrEmpty(tableField.id))
            {
                this.SetEntityId(tableField);
                addTables.Add(tableField);
            }
            else
                updateTables.Add(tableField);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addTables);

                this.fcpRepository.CommonRepository.UpdateEntities(updateTables);
            });
            await _fcpService.FormConfigVersionAsync(tableField.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteTableFieldFilterField(string id)
        {
            var tableField = await this.fcpRepository.CommonRepository.GetEntityAsync<t_table_field_filter_field>(id);
            await _fcpService.FormConfigVersionAsync(tableField.fcp_form_id);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_table_field_filter_field>(id);
            });
            return true;

        }
        #endregion


        #region tableFilterField

        private async Task<ApiPagedResult<t_table_filter_field>>
                 OrderPageTableFilterField(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_table_filter_field>(apiOrderPageParam, null);
            // await EntityToWebModelUtls.GetTableFieldLabelsAsync(r.Data.ToList(), apiOrderPageParam.Lang, this.fcpRepository.CommonRepository);
            return r;

        }


        private async Task<t_table_filter_field> GetTableFilterField(ApiParam apiParam)
        {
            var table = await this.fcpRepository.CommonRepository.GetEntityAsync<t_table_filter_field>(apiParam.Keyword);
            return table;

        }


        private async Task<bool> FullUpdateTableFilterField(t_table_filter_field tableField)
        {
            List<t_table_filter_field> addTables = new List<t_table_filter_field>();
            List<t_table_filter_field> updateTables = new List<t_table_filter_field>();
            if (string.IsNullOrEmpty(tableField.id))
            {
                this.SetEntityId(tableField);
                addTables.Add(tableField);
            }
            else
                updateTables.Add(tableField);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addTables);

                this.fcpRepository.CommonRepository.UpdateEntities(updateTables);
            });
            await _fcpService.FormConfigVersionAsync(tableField.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteTableFilterField(string id)
        {
            var tableField = await this.fcpRepository.CommonRepository.GetEntityAsync<t_table_filter_field>(id);
            await _fcpService.FormConfigVersionAsync(tableField.fcp_form_id);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_table_filter_field>(id);
            });
            return true;

        }
        #endregion



        #region tablePanel

        private async Task<ApiPagedResult<t_table_panel>>
                 OrderPageTablePanel(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_table_panel>(apiOrderPageParam, null);
            if (r != null && r.Data != null) await EntityToWebModelUtls.GetTablePanelLabelsAsync(r.Data.ToList(), this.lang, this.fcpRepository.CommonRepository);
            return r;

        }


        private async Task<t_table_panel> GetTablePanel(ApiParam apiParam)
        {
            var table = await this.fcpRepository.CommonRepository.GetEntityAsync<t_table_panel>(apiParam.Keyword);
            if (table != null)
            {
                var tables = new List<t_table_panel>();
                tables.Add(table);
                await EntityToWebModelUtls.GetTablePanelLabelsAsync(tables, this.lang, this.fcpRepository.CommonRepository);
            }
            return table;

        }


        private async Task<bool> FullUpdateTablePanel(t_table_panel tableField)
        {
            List<t_table_panel> addTables = new List<t_table_panel>();
            List<t_table_panel> updateTables = new List<t_table_panel>();
            List<t_label> addLabels = new List<t_label>();
            List<t_label> updateLabels = new List<t_label>();
            List<t_label_detail> addLabelDetails = new List<t_label_detail>();
            List<t_label_detail> updateLabelDetails = new List<t_label_detail>();
            if (string.IsNullOrEmpty(tableField.id))
            {
                this.SetEntityId(tableField);
                addTables.Add(tableField);
            }
            else
                updateTables.Add(tableField);
            t_label label = null;
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            if (!string.IsNullOrEmpty(tableField.label_label_id))
                label = await this.fcpRepository.CommonRepository.GetEntityAsync<t_label>(tableField.label_label_id);
            if (label == null)
            {
                label = new t_label();
                label.org_id = tableField.org_id;
                this.SetEntityId(label);
                addLabels.Add(label);
                tableField.label_label_id = label.id;
            }
            else
            {
                labelDetails = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_label_detail>(new ApiParam() { BeginTime = label.create_time },
                        t => t.label_id == label.id)).ToList();
            }

            var ld = labelDetails.FirstOrDefault(t => string.IsNullOrEmpty(t.lang) || t.lang == this.lang);
            if (ld == null)
            {
                ld = new t_label_detail();
                ld.org_id = label.org_id;
                ld.label_id = label.id;
                this.SetEntityId(ld);
                addLabelDetails.Add(ld);
            }
            else
                updateLabelDetails.Add(ld);
            ld.label = tableField.vir_label;
            ld.lang = this.lang;
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addTables);
                this.fcpRepository.CommonRepository.AddEntities(addLabels);
                this.fcpRepository.CommonRepository.AddEntities(addLabelDetails);

                this.fcpRepository.CommonRepository.UpdateEntities(updateTables);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabels);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabelDetails);
            });
            await _fcpService.FormConfigVersionAsync(tableField.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteTablePanel(string id)
        {
            var tablePanel = await this.fcpRepository.CommonRepository.GetEntityAsync<t_table_panel>(id);
            var tableFields = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_table_field>(t =>
                t.table_panel_id == tablePanel.id)).ToList();
            tableFields.ForEach(t => t.table_panel_id = null);
            await _fcpService.FormConfigVersionAsync(tablePanel.fcp_form_id);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_table_panel>(id);
                this.fcpRepository.CommonRepository.UpdateEntities(tableFields);
            });
            return true;

        }
        #endregion




        #region tableBtn

        private async Task<ApiPagedResult<t_table_btn>>
                 OrderPageTableBtn(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_table_btn>(apiOrderPageParam, null);
            if (r != null && r.Data != null) await EntityToWebModelUtls.GetTableBtnLabelsAsync(r.Data.ToList(), this.lang, this.fcpRepository.CommonRepository);
            return r;

        }


        private async Task<t_table_btn> GetTableBtn(ApiParam apiParam)
        {
            var table = await this.fcpRepository.CommonRepository.GetEntityAsync<t_table_btn>(apiParam.Keyword);
            if (table != null)
            {
                var tables = new List<t_table_btn>();
                tables.Add(table);
                await EntityToWebModelUtls.GetTableBtnLabelsAsync(tables, this.lang, this.fcpRepository.CommonRepository);
            }
            return table;

        }


        private async Task<bool> FullUpdateTableBtn(t_table_btn tableField)
        {
            List<t_table_btn> addTables = new List<t_table_btn>();
            List<t_table_btn> updateTables = new List<t_table_btn>();
            List<t_label> addLabels = new List<t_label>();
            List<t_label> updateLabels = new List<t_label>();
            List<t_label_detail> addLabelDetails = new List<t_label_detail>();
            List<t_label_detail> updateLabelDetails = new List<t_label_detail>();
            if (string.IsNullOrEmpty(tableField.id))
            {
                this.SetEntityId(tableField);
                addTables.Add(tableField);
            }
            else
                updateTables.Add(tableField);
            t_label label = null;
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            if (!string.IsNullOrEmpty(tableField.btn_label_id))
                label = await this.fcpRepository.CommonRepository.GetEntityAsync<t_label>(tableField.btn_label_id);
            if (label == null)
            {
                label = new t_label();
                label.org_id = tableField.org_id;
                this.SetEntityId(label);
                addLabels.Add(label);
                tableField.btn_label_id = label.id;
            }
            else
            {
                labelDetails = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_label_detail>(new ApiParam() { BeginTime = label.create_time },
                        t => t.label_id == label.id)).ToList();
            }

            var ld = labelDetails.FirstOrDefault(t => string.IsNullOrEmpty(t.lang) || t.lang == this.lang);
            if (ld == null)
            {
                ld = new t_label_detail();
                ld.org_id = label.org_id;
                ld.label_id = label.id;
                this.SetEntityId(ld);
                addLabelDetails.Add(ld);
            }
            else
                updateLabelDetails.Add(ld);
            ld.label = tableField.vir_btn_label;
            ld.lang = this.lang;
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addTables);
                this.fcpRepository.CommonRepository.AddEntities(addLabels);
                this.fcpRepository.CommonRepository.AddEntities(addLabelDetails);
                this.fcpRepository.CommonRepository.UpdateEntities(updateTables);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabels);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabelDetails);
            });
            await _fcpService.FormConfigVersionAsync(tableField.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteTableBtn(string id)
        {
            var tablePanel = await this.fcpRepository.CommonRepository.GetEntityAsync<t_table_btn>(id);
            await _fcpService.FormConfigVersionAsync(tablePanel.fcp_form_id);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_table_btn>(id);
            });
            return true;

        }
        #endregion


        #region tree

        private async Task<ApiPagedResult<t_tree>>
                 OrderPageTree(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_tree>(apiOrderPageParam, null);
            if (r != null && r.Data != null) await EntityToWebModelUtls.GetTreeLabelsAsync(r.Data.ToList(), this.lang, this.fcpRepository.CommonRepository);
            return r;

        }


        private async Task<t_tree> GetTree(ApiParam apiParam)
        {
            var tree = await this.fcpRepository.CommonRepository.GetEntityAsync<t_tree>(apiParam.Keyword);
            if (tree != null)
            {
                var trees = new List<t_tree>();
                trees.Add(tree);
                await EntityToWebModelUtls.GetTreeLabelsAsync(trees, this.lang, this.fcpRepository.CommonRepository);
            }
            return tree;

        }


        private async Task<bool> FullUpdateTree(t_tree tree)
        {
            List<t_tree> addTrees = new List<t_tree>();
            List<t_tree> updateTrees = new List<t_tree>();
            List<t_label> addLabels = new List<t_label>();
            List<t_label> updateLabels = new List<t_label>();
            List<t_label_detail> addLabelDetails = new List<t_label_detail>();
            List<t_label_detail> updateLabelDetails = new List<t_label_detail>();
            if (string.IsNullOrEmpty(tree.id))
            {
                this.SetEntityId(tree);
                addTrees.Add(tree);
            }
            else
                updateTrees.Add(tree);
            t_label label = null;
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            if (!string.IsNullOrEmpty(tree.label_label_id))
                label = await this.fcpRepository.CommonRepository.GetEntityAsync<t_label>(tree.label_label_id);
            if (label == null)
            {
                label = new t_label();
                label.org_id = tree.org_id;
                this.SetEntityId(label);
                addLabels.Add(label);
                tree.label_label_id = label.id;
            }
            else
            {
                labelDetails = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_label_detail>(new ApiParam() { BeginTime = label.create_time },
                        t => t.label_id == label.id)).ToList();
            }

            var ld = labelDetails.FirstOrDefault(t => string.IsNullOrEmpty(t.lang) || t.lang == this.lang);
            if (ld == null)
            {
                ld = new t_label_detail();
                ld.org_id = label.org_id;
                ld.label_id = label.id;
                this.SetEntityId(ld);
                addLabelDetails.Add(ld);
            }
            else
                updateLabelDetails.Add(ld);
            ld.label = tree.vir_label;
            ld.lang = this.lang;
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addTrees);
                this.fcpRepository.CommonRepository.AddEntities(addLabels);
                this.fcpRepository.CommonRepository.AddEntities(addLabelDetails);

                this.fcpRepository.CommonRepository.UpdateEntities(updateTrees);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabels);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabelDetails);
            });
            await _fcpService.FormConfigVersionAsync(tree.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteTree(string id)
        {
            var tree = await this.fcpRepository.CommonRepository.GetEntityAsync<t_tree>(id);

            var treeFieldIds = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_tree_field>(t =>
                     t.tree_id == tree.id)).Select(t => t.id).ToList();
            var treeBtnIds = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_tree_btn>(t =>
                 t.tree_id == tree.id)).Select(t => t.id).ToList();
            await _fcpService.FormConfigVersionAsync(tree.fcp_form_id);

             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_tree>(id);
                this.fcpRepository.CommonRepository.DeleteEntity<t_tree_field>(treeFieldIds);
                this.fcpRepository.CommonRepository.DeleteEntity<t_tree_btn>(treeBtnIds);
            });
            return true;

        }
        #endregion


        #region treeField

        private async Task<ApiPagedResult<t_tree_field>>
                 OrderPageTreeField(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_tree_field>(apiOrderPageParam, null);
            if (r != null && r.Data != null) await EntityToWebModelUtls.GetTreeFieldLabelsAsync(r.Data.ToList(), this.lang, this.fcpRepository.CommonRepository);
            return r;

        }


        private async Task<t_tree_field> GetTreeField(ApiParam apiParam)
        {
            var tree = await this.fcpRepository.CommonRepository.GetEntityAsync<t_tree_field>(apiParam.Keyword);
            if (tree != null)
            {
                var trees = new List<t_tree_field>();
                trees.Add(tree);
                await EntityToWebModelUtls.GetTreeFieldLabelsAsync(trees, this.lang, this.fcpRepository.CommonRepository);
            }
            return tree;

        }


        private async Task<bool> FullUpdateTreeField(t_tree_field treeField)
        {
            List<t_tree_field> addTrees = new List<t_tree_field>();
            List<t_tree_field> updateTrees = new List<t_tree_field>();
            List<t_label> addLabels = new List<t_label>();
            List<t_label> updateLabels = new List<t_label>();
            List<t_label_detail> addLabelDetails = new List<t_label_detail>();
            List<t_label_detail> updateLabelDetails = new List<t_label_detail>();
            if (string.IsNullOrEmpty(treeField.id))
            {
                this.SetEntityId(treeField);
                addTrees.Add(treeField);
            }
            else
                updateTrees.Add(treeField);
            t_label label = null;
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            if (!string.IsNullOrEmpty(treeField.field_label_id))
                label = await this.fcpRepository.CommonRepository.GetEntityAsync<t_label>(treeField.field_label_id);
            if (label == null)
            {
                label = new t_label();
                label.org_id = treeField.org_id;
                this.SetEntityId(label);
                addLabels.Add(label);
                treeField.field_label_id = label.id;
            }
            else
            {
                labelDetails = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_label_detail>(new ApiParam() { BeginTime = label.create_time },
                        t => t.label_id == label.id)).ToList();
            }

            var ld = labelDetails.FirstOrDefault(t => string.IsNullOrEmpty(t.lang) || t.lang == this.lang);
            if (ld == null)
            {
                ld = new t_label_detail();
                ld.org_id = label.org_id;
                ld.label_id = label.id;
                this.SetEntityId(ld);
                addLabelDetails.Add(ld);
            }
            else
                updateLabelDetails.Add(ld);
            ld.label = treeField.vir_field_label;
            ld.lang = this.lang;
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addTrees);
                this.fcpRepository.CommonRepository.AddEntities(addLabels);
                this.fcpRepository.CommonRepository.AddEntities(addLabelDetails);

                this.fcpRepository.CommonRepository.UpdateEntities(updateTrees);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabels);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabelDetails);
            });
            await _fcpService.FormConfigVersionAsync(treeField.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteTreeField(string id)
        {
            var treeField = await this.fcpRepository.CommonRepository.GetEntityAsync<t_tree_field>(id);
            await _fcpService.FormConfigVersionAsync(treeField.fcp_form_id);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_tree_field>(id);
            });
            return true;

        }
        #endregion



        #region treeBtn

        private async Task<ApiPagedResult<t_tree_btn>>
                 OrderPageTreeBtn(ApiOrderPageParam apiOrderPageParam)
        {
            var r = await this.fcpRepository.CommonRepository.GetOrderPageEntitiesAsync<t_tree_btn>(apiOrderPageParam, null);
            if (r != null && r.Data != null) await EntityToWebModelUtls.GetTreeBtnLabelsAsync(r.Data.ToList(), this.lang, this.fcpRepository.CommonRepository);
            return r;

        }


        private async Task<t_tree_btn> GetTreeBtn(ApiParam apiParam)
        {
            var tree = await this.fcpRepository.CommonRepository.GetEntityAsync<t_tree_btn>(apiParam.Keyword);
            if (tree != null)
            {
                var trees = new List<t_tree_btn>();
                trees.Add(tree);
                await EntityToWebModelUtls.GetTreeBtnLabelsAsync(trees, this.lang, this.fcpRepository.CommonRepository);
            }
            return tree;

        }


        private async Task<bool> FullUpdateTreeBtn(t_tree_btn treeField)
        {
            List<t_tree_btn> addTrees = new List<t_tree_btn>();
            List<t_tree_btn> updateTrees = new List<t_tree_btn>();
            List<t_label> addLabels = new List<t_label>();
            List<t_label> updateLabels = new List<t_label>();
            List<t_label_detail> addLabelDetails = new List<t_label_detail>();
            List<t_label_detail> updateLabelDetails = new List<t_label_detail>();
            if (string.IsNullOrEmpty(treeField.id))
            {
                this.SetEntityId(treeField);
                addTrees.Add(treeField);
            }
            else
                updateTrees.Add(treeField);
            t_label label = null;
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            if (!string.IsNullOrEmpty(treeField.btn_label_id))
                label = await this.fcpRepository.CommonRepository.GetEntityAsync<t_label>(treeField.btn_label_id);
            if (label == null)
            {
                label = new t_label();
                label.org_id = treeField.org_id;
                this.SetEntityId(label);
                addLabels.Add(label);
                treeField.btn_label_id = label.id;
            }
            else
            {
                labelDetails = (await this.fcpRepository.CommonRepository.GetEntitiesAsync<t_label_detail>(new ApiParam() { BeginTime = label.create_time },
                        t => t.label_id == label.id)).ToList();
            }

            var ld = labelDetails.FirstOrDefault(t => string.IsNullOrEmpty(t.lang) || t.lang == this.lang);
            if (ld == null)
            {
                ld = new t_label_detail();
                ld.org_id = label.org_id;
                ld.label_id = label.id;
                this.SetEntityId(ld);
                addLabelDetails.Add(ld);
            }
            else
                updateLabelDetails.Add(ld);
            ld.label = treeField.vir_btn_label;
            ld.lang = this.lang;
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.AddEntities(addTrees);
                this.fcpRepository.CommonRepository.AddEntities(addLabels);
                this.fcpRepository.CommonRepository.AddEntities(addLabelDetails);

                this.fcpRepository.CommonRepository.UpdateEntities(updateTrees);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabels);
                this.fcpRepository.CommonRepository.UpdateEntities(updateLabelDetails);
            });
            await _fcpService.FormConfigVersionAsync(treeField.fcp_form_id);
            return true;

        }


        private async Task<bool> DeleteTreeBtn(string id)
        {
            var treePanel = await this.fcpRepository.CommonRepository.GetEntityAsync<t_tree_btn>(id);
            await _fcpService.FormConfigVersionAsync(treePanel.fcp_form_id);
             await this.fcpRepository.CommitChangesByCommonAsync(() =>
            {
                this.fcpRepository.CommonRepository.DeleteEntity<t_tree_btn>(id);
            });
            return true;

        }
        #endregion
    }
}
